﻿using DapperExtensions;
using Nova_Common.Extend;
using Nova_DAL;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nova_BLL
{
    public class TBill_DelStorageBll
    {
        TBill_DelStorageDal dal = new TBill_DelStorageDal();

        public TBill_DelStorage GetByStockNo(string stockNo)
        {
            var predicate = Predicates.Field<TBill_DelStorage>(t => t.StockNo, Operator.Eq, stockNo);
            return dal.GetByField(predicate);
        }

        public bool InsertEntity(TBill_DelStorage entity)
        {
            return !dal.Insert(entity).IsEmpty();
        }

        public bool UpdateEntity(TBill_DelStorage entity)
        {
            return dal.Update(entity);
        }

        public bool InsertEntity(TBill_DelStorage delStorageEntity, List<TDelStorage_List> delStorageListEntity)
        {
            using (var tran = dal.TranStart())
            {
                using (var conn = dal.GetConnection())
                {
                    try
                    {
                        conn.Connection.Insert(delStorageEntity, tran);
                        conn.Connection.Insert<TDelStorage_List>(delStorageListEntity, tran);
                        tran.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        //throw new Exception(ex.ToString());
                         return false;
                    }
                }
            }
        }

        public bool DeleteEntity(string stockNo)
        {
            using (var tran = dal.TranStart())
            {
                using (var conn = dal.GetConnection())
                {
                    try
                    {
                        conn.Connection.Delete<TDelStorage_List>(Predicates.Field<TDelStorage_List>(t => t.StockNo, Operator.Eq, stockNo), tran);
                        conn.Connection.Delete<TBill_DelStorage>(Predicates.Field<TBill_DelStorage>(t => t.StockNo, Operator.Eq, stockNo), tran);
                        tran.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        //throw new Exception(ex.ToString());
                        return false;
                    }
                }
            }
        }
    }
}
